
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>definition/method · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="converter.html" />
    
    
    <link rel="prev" href="definition.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    快速入门
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../guides/project.html">
            
                <a href="../guides/project.html">
            
                    
                    项目结构和初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../guides/api.html">
            
                <a href="../guides/api.html">
            
                    
                    API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../guides/doc.html">
            
                <a href="../guides/doc.html">
            
                    
                    生成 API 文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../guides/client.html">
            
                <a href="../guides/client.html">
            
                    
                    生成客户端
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../concepts/cp.html">
            
                <a href="../concepts/cp.html">
            
                    
                    Consumer && Producer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../concepts/operator.html">
            
                <a href="../concepts/operator.html">
            
                    
                    Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../concepts/filter.html">
            
                <a href="../concepts/filter.html">
            
                    
                    Filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../concepts/prefab.html">
            
                <a href="../concepts/prefab.html">
            
                    
                    Prefab
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../concepts/modifier.html">
            
                <a href="../concepts/modifier.html">
            
                    
                    Modifier
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../concepts/middleware.html">
            
                <a href="../concepts/middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../concepts/context.html">
            
                <a href="../concepts/context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../concepts/source.html">
            
                <a href="../concepts/source.html">
            
                    
                    Parameter Generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../concepts/destination.html">
            
                <a href="../concepts/destination.html">
            
                    
                    Destination Handler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../concepts/error.html">
            
                <a href="../concepts/error.html">
            
                    
                    错误包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../concepts/method.html">
            
                <a href="../concepts/method.html">
            
                    
                    方法包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../concepts/configurer.html">
            
                <a href="../concepts/configurer.html">
            
                    
                    配置器机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../concepts/plugin.html">
            
                <a href="../concepts/plugin.html">
            
                    
                    插件机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../concepts/clients.html">
            
                <a href="../concepts/clients.html">
            
                    
                    多客户端整合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    插件
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../plugins/logger.html">
            
                <a href="../plugins/logger.html">
            
                    
                    系统日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../plugins/reqlog.html">
            
                <a href="../plugins/reqlog.html">
            
                    
                    请求日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../plugins/metrics.html">
            
                <a href="../plugins/metrics.html">
            
                    
                    监控指标插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../plugins/tracing.html">
            
                <a href="../plugins/tracing.html">
            
                    
                    请求追踪插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../plugins/profiling.html">
            
                <a href="../plugins/profiling.html">
            
                    
                    性能分析插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../plugins/version.html">
            
                <a href="../plugins/version.html">
            
                    
                    版本信息插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../plugins/healthcheck.html">
            
                <a href="../plugins/healthcheck.html">
            
                    
                    健康检查插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    框架开发者指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="start.html">
            
                <a href="start.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="log.html">
            
                <a href="log.html">
            
                    
                    log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="errors.html">
            
                <a href="errors.html">
            
                    
                    errors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="definition.html">
            
                <a href="definition.html">
            
                    
                    definition
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.5" data-path="method.html">
            
                <a href="method.html">
            
                    
                    definition/method
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="converter.html">
            
                <a href="converter.html">
            
                    
                    operators/converter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="validator.html">
            
                <a href="validator.html">
            
                    
                    operators/validator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="router.html">
            
                <a href="router.html">
            
                    
                    service/router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="service.html">
            
                <a href="service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="nirvana.html">
            
                <a href="nirvana.html">
            
                    
                    nirvana
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="config.html">
            
                <a href="config.html">
            
                    
                    config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="rest.html">
            
                <a href="rest.html">
            
                    
                    rest
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="utils.html">
            
                <a href="utils.html">
            
                    
                    utils
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="command.html">
            
                <a href="command.html">
            
                    
                    cmd/nirvana
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >definition/method</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="method-&#x5305;">method &#x5305;</h1>
<p>&#x5728; definition &#x5305;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x5230; Definition.Function &#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x5C31;&#x8981;&#x6C42;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x5FC5;&#x987B;&#x653E;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x800C;&#x4E0D;&#x80FD;&#x5728;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x4E2D;&#x3002;&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;method &#x5305;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x5BB9;&#x5668;&#xFF0C;&#x5E2E;&#x52A9;&#x7528;&#x6237;&#x628A;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x8F6C;&#x6362;&#x4E3A;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x5BB9;&#x5668;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Container contains instances and mappings.</span>
<span class="hljs-keyword">type</span> Container <span class="hljs-keyword">struct</span> {
    ...
}

<span class="hljs-comment">// Put puts an instance in this container. The instance must have one or more methods.</span>
<span class="hljs-keyword">func</span> (c *Container) Put(instance <span class="hljs-keyword">interface</span>{})

<span class="hljs-comment">// PutInterface puts an instance in this container. The instance must have one or more methods.</span>
<span class="hljs-comment">// The iface should be like (*ArbitraryInterface)(nil).</span>
<span class="hljs-keyword">func</span> (c *Container) PutInterface(iface <span class="hljs-keyword">interface</span>{}, instance <span class="hljs-keyword">interface</span>{})

<span class="hljs-comment">// Get returns a function for specified method. If you want to specify a method from an</span>
<span class="hljs-comment">// interface, you need to use (*ArbitraryInterface)(nil) as instance.</span>
<span class="hljs-keyword">func</span> (c *Container) Get(instance <span class="hljs-keyword">interface</span>{}, method <span class="hljs-keyword">string</span>) <span class="hljs-keyword">interface</span>{}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x5BB9;&#x5668;&#x5206;&#x79BB;&#x4E86;&#x65B9;&#x6CD5;&#x7684; Get &#x548C; Put &#x8FC7;&#x7A0B;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x53EF;&#x4EE5;&#x5728;&#x58F0;&#x660E; API &#x65F6;&#xFF0C;&#x4F7F;&#x7528; Get &#x83B7;&#x53D6;&#x67D0;&#x4E2A;&#x5B9E;&#x4F8B;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4E4B;&#x540E;&#x518D;&#x5728;&#x670D;&#x52A1;&#x542F;&#x52A8;&#x903B;&#x8F91;&#x91CC; Put &#x771F;&#x6B63;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x5373; Get &#x53EF;&#x4EE5;&#x5728; Put &#x4E4B;&#x524D;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x4E3B;&#x8981;&#x662F;&#x5229;&#x7528;&#x4E86; golang &#x7684;&#x52A8;&#x6001;&#x51FD;&#x6570;&#x6784;&#x5EFA;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5148;&#x751F;&#x6210;&#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x51FD;&#x6570;&#x88AB;&#x771F;&#x6B63;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#x624D;&#x53BB;&#x83B7;&#x53D6;&#x771F;&#x6B63;&#x7684;&#x5B9E;&#x4F8B;&#x91CC;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<p>method &#x5305;&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x7684; Container&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> defaultContainer = NewContainer()

<span class="hljs-comment">// Put puts an instance in this container. The instance must have one or more methods.</span>
<span class="hljs-keyword">func</span> Put(instance <span class="hljs-keyword">interface</span>{}) {
    defaultContainer.Put(instance)
}

<span class="hljs-comment">// PutInterface puts an instance in this container. The instance must have one or more methods.</span>
<span class="hljs-comment">// The iface should be like (*ArbitraryInterface)(nil).</span>
<span class="hljs-keyword">func</span> PutInterface(iface <span class="hljs-keyword">interface</span>{}, instance <span class="hljs-keyword">interface</span>{}) {
    defaultContainer.PutInterface(iface, instance)
}

<span class="hljs-comment">// Get returns a function for specified method. If you want to specify a method from an</span>
<span class="hljs-comment">// interface, you need to use (*ArbitraryInterface)(nil) as instance.</span>
<span class="hljs-keyword">func</span> Get(instance <span class="hljs-keyword">interface</span>{}, method <span class="hljs-keyword">string</span>) <span class="hljs-keyword">interface</span>{} {
    <span class="hljs-keyword">return</span> defaultContainer.Get(instance, method)
}
</code></pre>
<p>&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x7528;&#x6237;&#x4F1A;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x5168;&#x5C40;&#x7684;&#x5BB9;&#x5668;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="definition.html" class="navigation navigation-prev " aria-label="Previous page: definition">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="converter.html" class="navigation navigation-next " aria-label="Next page: operators/converter">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"definition/method","level":"1.5.5","depth":2,"next":{"title":"operators/converter","level":"1.5.6","depth":2,"path":"topics/converter.md","ref":"topics/converter.md","articles":[]},"previous":{"title":"definition","level":"1.5.4","depth":2,"path":"topics/definition.md","ref":"topics/definition.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*"},"file":{"path":"topics/method.md","mtime":"2019-08-06T09:48:58.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-20T10:33:16.586Z"},"basePath":"..","book":{"language":"zh-hans"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

